require "Common/define"
require "Logic/Config"
require "Common/functions"

UIRedDots = {};
HelperFunc.SetUIEnv(UIRedDots)
local this = UIRedDots;
this.preObj = nil;
this.controller = nil;--0空 1 弱提示 2强提示
this.datas = {};
this.cacheDatas={}; --预制件未加载出时缓存数据

this.viewToNames = {} -- UI界面对应的红点

function UIRedDots.onCreate(obj)
    preObj = obj;
    controller = obj:GetController("c1");
    controller.selectedIndex = 0;
    --preObj:SetActive(false);
    for key, value in pairs(cacheDatas) do
        local data = this.createData(value.go);
        datas[key] = data;
        for i = 1, #value.infos do
            this.registData(data,value.infos[i].type,value.infos[i].param);
        end
    end
    cacheDatas={};
end

function UIRedDots.addViewListen(viewName,name,go,type,...)
    if this.viewToNames[viewName] == nil then 
        this.viewToNames[viewName] = {}
    end
    local names = this.viewToNames[viewName]
    local flag = true
    for k,v in pairs(names) do
        if v.name == name and v.type == type then
            flag = false
            break
        end
    end
    if flag then
        table.insert(this.viewToNames[viewName],{name=name,type=type})
    end
    this.addListen(name,go,type,...)
end

function UIRedDots.removeViewEvent(viewName)
    if this.viewToNames[viewName] == nil then 
       return 
    end
    for key, value in pairs(this.viewToNames[viewName]) do
        this.removeListen(value.name,value.type)
    end
    table.clear(this.viewToNames[viewName])
end

function UIRedDots.addListen(name,go,type,...)
    if name == nil or go == nil or type == nil  then
        return;
    end
    if go~=nil and not logicMgr.FuncOpenManager.checkItemState(go) then
        return;
    end
    if preObj~=nil then
        local data = datas[name];
        if data == nil then
            data =  this.createData(go);
            datas[name] =data;
        end
        local redDotsTr = go:GetChild("RedDots");
        if redDotsTr~=nil then
            --redDotsTr:SetAsLastSibling();
            go = redDotsTr;
        end
        if  data.go~=nil and data.go~=go:GetChild("UIRedDots") then
            go:AddChild(data.go);
            --data.go.x = 0;
            --data.go.y = 0;
            --data.go:SetScale(0,0);
            data.go.rotation = 0;
            data.go.rotationX = 0;
            data.go.rotationY = 0;
        end

        this.registData(data,type,{...});
    else
        local data = cacheDatas[name];
        if data == nil then
            data = {go =go,infos={}};
            cacheDatas[name] =data;
        end
        local isUnRecord =true;
        for i = 1, #data.infos do
            if data.infos[i].type == type then
                data.infos[i].param = {...}
                isUnRecord = false;
                break;
            end
        end
        if isUnRecord then
            table.insert(data.infos,{type=type,param={...}});
        end
        if #cacheDatas <=1 then
            local name, package = getUIName(PanelNames.UIRedDots);
            panelMgr:ShowUI(name, package, this.onCreate, Config.useLocalUI);
        end
    end
end

function UIRedDots.removeListen(name,type) --type为nil代表移除所有
    if name==nil then
        return;
    end

    local data = datas[name];
    if data~=nil then
        this.unRegistData(data,type);
        if #data.redInfos ==0 then
            datas[name] = nil;
        end
    else
        if type == nil then
            cacheDatas[name] = nil;
        elseif cacheDatas[name]~=nil then
            data = cacheDatas[name];
            for i = 1, #data.infos do
                if data[i].type == type then
                    table.remove(data,i);
                    break;
                end
            end
        end
    end
end


function UIRedDots.createData(go)
    local data = {};
    local redDotsTr = go:GetChild("RedDots");
    if redDotsTr~=nil then
        --redDotsTr:SetAsLastSibling();
        go = redDotsTr;
    end
    -- if go:GetChild(preObj.gameObjectName)~= nil then
    data.go = go:GetChild(preObj.gameObjectName)
    -- else
    if data.go == nil then
        local name, package = getUIName(PanelNames.UIRedDots);
        data.go = newGComponent(name,package);
        data.go.name = go.gameObjectName;
        go:AddChild(data.go);
        data.go:SetPosition( go.width,Vector3.zero.y,Vector3.zero.z);
        data.go:SetScale(Vector3.one.x,Vector3.one.y);
        data.go.rotation = 0;
        data.go.rotationX = 0;
        data.go.rotationY = 0;
    end
    data.controller = data.go:GetController("c1");
    if data.controller then
        data.controller.selectedIndex = 0;
    else
        logError(go.name .. "下红点控制器为空")
    end
    
    data.redInfos ={};

    return data;
end

function UIRedDots.registData(redData,type,param)
    local redInfo = nil;
    for i = 1, #redData.redInfos do
        if redData.redInfos[i].redType == type then
            redInfo = redData.redInfos[i];
            break;
        end
    end
    if redInfo == nil then
        redInfo ={};
        redInfo.redType =type;
        redInfo.func =function ()
                redInfo.state = RedDotsManager.getFlagState(redInfo.redType,unpack(redInfo.param));
                this.refreshRedDots(redData);
            end
        RedDotsManager.addEventListen(redInfo.redType,redInfo.func);
        table.insert(redData.redInfos,redInfo);
    end

    redInfo.param = param;
    redInfo.state = Config.RedDotsFlagState.None;
    redInfo.func();
end

function UIRedDots.unRegistData(redData,type)
    if type~=nil then
        for i = 1, #redData.redInfos do
            if redData.redInfos[i].redType == type then
                RedDotsManager.removeEventListen(type,redData.redInfos[i].func);
                table.remove(redData.redInfos,i);
                break;
            end
        end
    else
        for i = 1, #redData.redInfos do
            if redData.redInfos[i].redType == type then
                RedDotsManager.removeEventListen(type,redData.redInfos[i].func);
            end
        end
        redData.redInfos ={};
    end
    if #redData.redInfos ==0 then
        if redData.controller then
            redData.controller.selectedIndex = 0;
        end
        redData.go = nil;
        redData.controller = nil;
    else
        this.refreshRedDots(redData);
    end
end


function UIRedDots.GetRedDotsStateByName(name)
    if name == nil then
        return Config.RedDotsFlagState.None;
    end
    local data = datas[name];
    if data == nil then
        return Config.RedDotsFlagState.None;
    end
    for i = 1, #data.redInfos do
        if data.redInfos[i].state ~= Config.RedDotsFlagState.None then
            return data.redInfos[i].state;
        end
    end
    return Config.RedDotsFlagState.None;
end

function UIRedDots.refreshRedDots(redData)
    if redData.go~=nil then
        local finishState =Config.RedDotsFlagState.None;
        for i = 1, #redData.redInfos do
            if redData.redInfos[i].state>finishState then
                finishState = redData.redInfos[i].state;
            end
            if redData.redInfos[i].type == 30 then
                local a = 1;
            end
        end
        if redData.controller then
            redData.controller.selectedIndex =  finishState;
        end
    end
end

function UIRedDots.Destory()
    preObj = nil;
    controller = nil;--0空 1 弱提示 2强提示
    datas = {};
    cacheDatas={}; --预制件未加载出时缓存数据
end

return UIRedDots